/**
 * 
 */
package com.thor.palmmall.rest.device.repair;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.palmmall.rest.common.AAssignInfo;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SAssignInfoConverter;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.device.repair.converter.ADeviceRepairConfigConverter;
import com.thor.palmmall.rest.device.repair.converter.ADeviceRepairConverter;
import com.thor.palmmall.rest.device.repair.converter.SDeviceRepair4CreateConverter;
import com.thor.palmmall.rest.device.repair.converter.SDeviceRepairConverter;
import com.thor.palmmall.rest.repair.ARepairBillQueryParam;
import com.thor.palmmall.rest.repair.converter.SMaintainBillQueryParamConverter;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.device.repair.SDeviceRepair;
import com.thor.palmmall.spi.device.repair.SDeviceRepairConfig;
import com.thor.palmmall.spi.device.repair.SDeviceRepairService;

/**
 * @author NuoSa
 *
 * @since 1.0
 */
public class ADeviceRepairServiceImpl implements ADeviceRepairService {

  @Override
  public String create(ADeviceRepair4Create bill, BeanOperateInfo operInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operInfo, "operateInfo");
    Assert.assertArgumentNotNull(operInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = sDeviceRepairService.create(
        SDeviceRepair4CreateConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法创建安保设备维修单：" + resp.getMessage());
  }

  @Override
  public ADeviceRepair get(String id, AUserGroupLoadParam param) throws ThorServiceException {
    Assert.assertArgumentNotNull(id, "id");
    SResponse<SDeviceRepair> resp = sDeviceRepairService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return ADeviceRepairConverter.getInstance().convert(resp.getBody());
    else
      throw new ThorServiceException("无法找到指定ID的安保设备维修单：" + resp.getMessage());
  }

  @Override
  public String save(ADeviceRepair bill, BeanOperateInfo operInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operInfo, "operateInfo");
    Assert.assertArgumentNotNull(operInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = sDeviceRepairService.save(
        SDeviceRepairConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法保存安保设备维修单：" + resp.getMessage());
  }

  @Override
  public QueryResult<ADeviceRepair> query(ARepairBillQueryParam queryParam)
      throws ThorServiceException {
    SResponse<SQueryResult<SDeviceRepair>> resp = sDeviceRepairService
        .query(SMaintainBillQueryParamConverter.getInstance().convert(queryParam));
    if (resp.isSuccess())
      return SQueryResultConverter.convert(resp.getBody(), ADeviceRepairConverter.getInstance());
    else
      throw new ThorServiceException("无法查询安保设备维修单：" + resp.getMessage());
  }

  @Override
  public void assign(AAssignInfo assignInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(assignInfo, "assignInfo");

    SResponse<Void> resp = sDeviceRepairService
        .assign(SAssignInfoConverter.getInstance().convert(assignInfo));
    if (resp.isSuccess())
      return;
    else
      throw new ThorServiceException("指派安保设备维修单失败：" + resp.getMessage());
  }

  @Override
  public String[] queryOutgoingState() throws ThorServiceException {
    SResponse<String[]> resp = sDeviceRepairService.queryOutgoingState();
    if (resp.isSuccess()) {
      return resp.getBody();
    } else {
      throw new ThorServiceException("查询流程状态失败：" + resp.getMessage());
    }
  }

  @Override
  public ADeviceRepairConfig getConfig(String storeUuid) throws ThorServiceException {
    Assert.assertArgumentNotNull(storeUuid, "storeUuid");

    SResponse<SDeviceRepairConfig> resp = sDeviceRepairService.getConfig(storeUuid);
    if (resp.isSuccess()) {
      return ADeviceRepairConfigConverter.getInstance().convert(resp.getBody());
    } else
      throw new ThorServiceException("无法找到指定的安保维修单配置：" + resp.getMessage());
  }

  @Autowired
  private SDeviceRepairService sDeviceRepairService;
}
